//
//  SunSecondCell.m
//  Xspcf
//
//  Created by sunshine on 16/10/12.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunSecondCell.h"
#import <test/SunCycleProgress.h>
#import "ColorButton.h"
#undef Y
#define Y(y) AY(y)
@interface SunSecondCell()
{
    UILabel *_title;
    UIImageView *_stateSignal;
    UILabel *_basePercent;
    UIImageView *_addSignal;
    UILabel *_days;
    UILabel *_startMoney;
    UILabel *_totalMoney;
}
@property (nonatomic, strong) UILabel *seasonLable;
@property (nonatomic, strong) UILabel *progressLable;
@property (nonatomic, strong) UIView *hideView;
@property (nonatomic, strong) UIImageView *progressView;
@property (nonatomic, strong) UIImageView *selledView;

@end
@implementation SunSecondCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"cellID";
    SunSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[SunSecondCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell creatSubviews];
    }
    return cell;
}

- (void)creatSubviews{
    self.backgroundColor = [UIColor whiteColor];
    
    _title = [[UILabel alloc]init];
    [self addSubview:_title];
    [_title lt_setLeft:X(23) to:nil save:YES];
    [_title lt_setTop:7 to:nil save:YES];
    _title.font = [UIFont systemFontOfSize:12];
    _title.text = @"正在连接网络，请稍候。。。";
    _title.textColor = [UIColor colorWithHexString:@"000000"];
    
    self.seasonLable = [[UILabel alloc]init];
    [self addSubview:self.seasonLable];
    [self.seasonLable lt_setLeft:X(2) to:_title save:YES];
    [self.seasonLable lt_setTop:7 to:nil save:YES];
    self.seasonLable.font = [UIFont systemFontOfSize:12];
    self.seasonLable.text = @"正在连接网络，请稍候。。。";
    self.seasonLable.textColor = [UIColor colorWithHexString:@"000000"];
    
    _stateSignal = [[UIImageView alloc]init];
    [self addSubview:_stateSignal];
    _stateSignal.image = [UIImage imageNamed:@"second_activity"];
    [_stateSignal lt_setSize:CGSizeMake(77, 37) save:YES];
    [_stateSignal lt_setTop:0 to:nil save:YES];
    [_stateSignal lt_setRight:X(14) to:nil save:YES];
    UILabel *state = [[UILabel alloc]init];
    [_stateSignal addSubview:state];
    [state setFrame:CGRectMake(10, 0, 150, 30)];
    state.tag = 1;
    state.textColor = WHITE;
    state.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];;
//    state.textAlignment = NSTextAlignmentCenter;
    state.text = @"投资好礼";
    
    _basePercent = [[UILabel alloc]init];
    [self addSubview:_basePercent];
    [_basePercent lt_setLeft:X(23) to:nil save:YES];
    [_basePercent lt_setTop:18 to:_title save:YES];
    _basePercent.text = @"0.00%";
    _basePercent.textColor = [UIColor colorWithHexString:@"FA7060"];
    _basePercent.font = [UIFont fontWithName:@"DINCondensed-Bold" size:28];;
    [_basePercent setMarkup:@"<font size=\"28\">0.00</font><font size=\"18\">%</font>"];
    
    _days = [[UILabel alloc]init];
    [self addSubview:_days];
    [_days setFrame:CGRectMake(X(166), 40, X(150), 40)];
    _days.font = [UIFont fontWithName:@"DINCondensed-Bold" size:28];;
    _days.textColor = [UIColor colorWithHexString:@"5A5A5A"];
    [_days setMarkup:@"<font size=\"28\">0</font><font size=\"18\">天</font>"];
    _startMoney = [[UILabel alloc]init];
    [self addSubview:_startMoney];
    [_startMoney lt_setLeft:X(23) to:nil save:YES];
    [_startMoney lt_setTop:1 to:_basePercent save:YES];
    _startMoney.font = [UIFont systemFontOfSize:11];
    _startMoney.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    _startMoney.text = @"0元起投";
    _totalMoney = [[UILabel alloc]init];
    [self addSubview:_totalMoney];
    [_totalMoney setFrame:CGRectMake(X(165), 81, X(77), 13)];
    _totalMoney.font = [UIFont systemFontOfSize:11];
    _totalMoney.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    _totalMoney.text = @"融资金额0万";
   
    // 进度条
    UIImage* backImage = [Sun imageWithColor:[UIColor colorWithHexString:@"DFBD7A"]];
    self.progressView = [[UIImageView alloc]initWithImage:backImage];
    [self.progressView setFrame:CGRectMake(X(21), 100, X(300), 3)];
    [self addSubview:self.progressView];
    self.hideView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, X(300), 3)];
    self.hideView.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];
    [self.progressView addSubview:self.hideView];
    
    // 进度标志
    self.progressLable = [[UILabel alloc]initWithFrame:CGRectMake(X(326), 93, 40, 14)];
    self.progressLable.textAlignment = NSTextAlignmentCenter;
    self.progressLable.text = @"00%";
    self.progressLable.font = [UIFont systemFontOfSize:12];
    self.progressLable.textColor = [UIColor colorWithHexString:@"DFBD7A"];
    [self addSubview:self.progressLable];

    //售罄标志
    self.selledView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"second_selled_logo"]];
    [self.selledView setFrame:CGRectMake(SCREENWIDTH - 58, 8, 58, 21)];
    [self addSubview:self.selledView];
    self.selledLabel = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 50, 21)];
    self.selledLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:13];
    self.selledLabel.textColor = WHITE;
    [self.selledView addSubview:self.selledLabel];
}

- (void)setCellData:(NSDictionary *)data{
    
    _title.text = [data objectForKey:@"name"];
    self.seasonLable.text = [data objectForKey:@"number"];
    if ([[data allKeys] containsObject:@"desc"]) {
        if ([[data objectForKey:@"desc"] length] > 0) {
            _stateSignal.hidden = NO;
            UILabel *state = [_stateSignal viewWithTag:1];
            state.text = [data objectForKey:@"desc"];
        }else {
            _stateSignal.hidden = YES;
        }
    }else {
        _stateSignal.hidden = YES;
    }
    [_days setMarkup:[NSString stringWithFormat:@"<font size=\"28\">%@</font><font size=\"18\">%@</font>",[data objectForKey:@"days"],[data objectForKey:@"time"]]];
    [_basePercent setMarkup:[NSString stringWithFormat:@"<font size=\"28\">%.2f</font><font size=\"18\">%%</font>",[[data objectForKey:@"aprBase"]doubleValue]]];
    // 进度
    double progress = [[data objectForKey:@"progress"]doubleValue];
    self.progressLable.text = [NSString stringWithFormat:@"%.0f%%",progress * 100];
    [self.hideView setFrame:CGRectMake(X(300)*progress, 0, X(300)-X(300)*progress, 3)];


    int total = [[data objectForKey:@"leaveMoney"]floatValue];
    if (total >= 10000) {
        _totalMoney.text = [NSString stringWithFormat:@"剩余%.2f万元",[[data objectForKey:@"leaveMoney"] floatValue]/10000.0];
    }else{
        _totalMoney.text = [NSString stringWithFormat:@"剩余%d元",[[data objectForKey:@"leaveMoney"]intValue]];
    }
    int lowest = [[data objectForKey:@"limitMoney"]intValue];
    if (lowest >= 10000) {
        _startMoney.text = [NSString stringWithFormat:@"%g万元起投",[[data objectForKey:@"limitMoney"]intValue]/10000.0];
    }else{
        _startMoney.text = [NSString stringWithFormat:@"%d元起投",[[data objectForKey:@"limitMoney"]intValue]];
    }
    // 售罄
    if ([[data objectForKey:@"status"]intValue] == 0) {
        _stateSignal.image = [UIImage imageNamed:@"state_sign_unable"];
        _stateSignal.hidden = YES;
        _title.textColor = UNABLE;
        self.seasonLable.textColor = UNABLE;
        _days.textColor = UNABLE;
        _basePercent.textColor = UNABLE;
        _addSignal.image = [UIImage imageNamed:@"percent_bg_unable"];
        self.progressLable.textColor = UNABLE;
        UIImage* backImage = [Sun imageWithColor:UNABLE];
        [self.progressView setImage:backImage];
        self.selledView.hidden = NO;
    }else{
        _stateSignal.image = [UIImage imageNamed:@"second_activity"];
        _title.textColor = [UIColor colorWithHexString:@"000000"];
        self.seasonLable.textColor = [UIColor colorWithHexString:@"000000"];
        _days.textColor = [UIColor colorWithHexString:@"5A5A5A"];
        _basePercent.textColor = [UIColor colorWithHexString:@"FA7060"];
        _addSignal.image = [UIImage imageNamed:@"percent_bg"];
        UIImage* backImage = [Sun imageWithColor:[UIColor colorWithHexString:@"DFBD7A"]];
        [self.progressView setImage:backImage];
        self.selledView.hidden = YES;
    }
    // 加息
    if ([[data objectForKey:@"aprAdd"]doubleValue] > 0){
        [_basePercent setMarkup:[NSString stringWithFormat:@"<font size=\"28\">%.2f</font><font size=\"18\">%%</font><font size=\"15\">+%.2f%%</font>",[[data objectForKey:@"aprBase"]doubleValue],[[data objectForKey:@"aprAdd"]doubleValue]]];
        [_days setFrame:CGRectMake(X(190), 35, X(77), 40)];
        [_totalMoney setFrame:CGRectMake(X(190), 71, X(177), 13)];

    }else {
        [_days setFrame:CGRectMake(X(160), 35, X(77), 40)];
        [_totalMoney setFrame:CGRectMake(X(160), 71, X(177), 13)];

    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
